#pragma once 

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>




class Util
{
    public:
    //读取一行http请求部分
    static int readOneLine(int sock, std::string& out)
    {
        
        //由于不同操作系统换行符表示方法不同 window:/r/n  linux:/n ，同一转为/n换行
        char ch = 'X';
        while ( ch != '\n')
        {
            ssize_t size = recv(sock, &ch, 1, 0);
            if(0 < size)
            {
                if(ch == '\r')
                {
                    size = recv(sock, &ch, 1, MSG_PEEK); //预读
                    if(ch == '\n')
                    { 
                        recv(sock, &ch, 1, 0) ;
                       // break;
                    }
                    else
                    {
                        ch = '\n' ;
                    }
                }
                out.push_back(ch);
            }
            else if (0 == size)
            {
                // 对端关闭连接
                return 0 ;
            }
            else
            {
                //接收失败
                return -1 ; 
            }
        }
        return out.size();
    }

    //截取header为key_value值
    static bool cutString(const std::string &target, std::string& out1, std::string& out2, std::string sep)
    {
        int pos = target.find(sep);
        if(pos != std::string::npos)
        {
            out1 = target.substr(0, pos);
            out2 = target.substr(pos + sep.size()) ;
            return true ;
        }
        return false ; 
    }


};



